package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.display.view.ICartView;
import com.shiguo365.market.presenter.contract.ICartPresenter;

import java.util.List;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;
/**
 * Created by CoderA on 2016/7/10.
 */
public class CartPresenter implements ICartPresenter {

    private static final String TAG = "CartPresenter";
    private final Activity mActivity;
    private final ICartView mCartView;

    public CartPresenter(@NonNull Activity activity, @NonNull ICartView cartView) {
        this.mActivity = activity;
        this.mCartView = cartView;
    }

    @Override
    public void getCartById(Integer uid) {
        AEClient _client = new AEClient("shiguo.cart.getByUid");
        _client.call(new AEParam().set("uid",uid),new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.d(TAG,jsonObject.toString());
                mCartView.onGetCartOk(jsonObject);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
                mCartView.onGetCartFailed();
//                if(Integer.valueOf(errno)==1011){
//
//                }
            }

            @Override
            public void onFinally(String s) {
                mCartView.onGetCartFinished();
            }
        });
    }

    @Override
    public void deleteCart(Integer uid, Integer pid) {
        AEClient _client = new AEClient("shiguo.cart.deleteCart");

        final AEParam aeParam =new AEParam().set("uid",uid).set("pid",pid);

        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.d(TAG,jsonObject.toString());
                Integer uid=(Integer) aeParam.get("uid");
                getCartById(uid);
                mCartView.onDelCartOk();

                //mCartView.onGetCartOk(jsonObject);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                mCartView.onGetCartFinished();
            }
        });
    }

    @Override
    public void updateCartNumber(Integer uid, Integer pid, Integer num,Integer num_all) {
        AEClient _client = new AEClient("shiguo.cart.updateCartNumber");
        final AEParam aeParam;
        if(num_all==null){
            aeParam=new AEParam().set("uid",uid).set("pid",pid).set("num",num);

        }else{
            aeParam =new AEParam().set("uid",uid).set("pid",pid).set("all_num",num_all);
        }


        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.d(TAG,jsonObject.toString());
                Integer uid=(Integer) aeParam.get("uid");
                getCartById(uid);
                //mCartView.onGetCartOk(jsonObject);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                mCartView.onGetCartFinished();
            }
        });
    }
}
